﻿using UnityEngine;
using UnityEngine.Events;
using System.Collections;
using System;
using com.gzc.zgxq.view;
using com.gzc.zgxq.game;

namespace SocialPoint.Examples.MVC {

    public class GameController {

        public static GameController instance;

        #region MODEL

        TimerModel m_timer { get; set; }
        /// <summary>
       /// AI走棋
       /// </summary>
       public ChessAiModel m_ai { get; private set; }
        PlayerModel m_playerModel { get; set; }

        GameModel m_gameModel { get; set; }

        #endregion MODEL

        #region VIEW

        UguiWindowViewPresenter m_windowViewPresenter { get; set; }
        
        AiViewPresenter m_aiViewPresenter { get; set; }
        ChessViewPresenter m_chessViewPresenter { get; set; }

        #endregion VIEW

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="windowName"></param>
        public GameController (string windowName) {
            InitTimer( );
            InitView(windowName);
            

            InitGameModel( );            
            InitAiModel( );
            InitPlayerModel( );

            InitChessGoPos( );

            instance = this;
        }

        private void InitTimer ( ) {
            m_timer = new TimerModel( );
        }

        void InitGameModel ( ) {
            // data model
            m_gameModel = new GameModel( );

            // model event
            m_gameModel.NewGameEvent += ( ) => { 
                Debuger.Log("NewGame业务逻辑");
            };

            m_gameModel.StartGameEvent += ( ) => {
                Debuger.Log("StartGame业务逻辑");
                // 响应玩家的输入
                //m_playerRootViewPresenter.Enable( );
                PlayerChessOnceMove( );

                // 开始计时器
                m_timer.StartTimer( );
            };

            m_gameModel.PauseGameEvent += ( ) => {
                Debuger.Log("PauseGame业务逻辑");
                // 停止响应玩家的输入
                //m_playerRootViewPresenter.Disable( );
                // 暂停计时器
                m_timer.PauseTimer( );
                // 停止AI下棋
            };

            m_gameModel.GameOverEvent += ( ) => {
                Debuger.Log("GameOver业务逻辑");

                // 停止计时器
                m_timer.StopTimer( );
            };
        }

        private void InitChessGoPos ( ) {
            m_chessViewPresenter.TraverseViewIndexSphere(( index ) => {
                m_chessViewPresenter.SetHashIndexCheess(index, m_chessViewPresenter.GetChessTypeGo(ChessMoveSearchEnginMain.s_chessBordArr[index]));
            });
            // 摆好所有棋子位置
            m_chessViewPresenter.InitAllChessPos( );
        }
            
        void InitAiModel ( ) {
            m_ai = new ChessAiModel( );
            //  电脑走一步棋后，回调让玩家走一步棋       
            AiChessOnceMove( );
        }

        void InitPlayerModel ( ) {
            m_playerModel = new PlayerModel( );

            //  玩家走一步棋后，回调让电脑走一步棋
            m_playerModel.PlayerOnceMoveFinishEvent += ( ) => {
                m_ai.AiOnceMove( );
                // 禁用玩家走棋drag棋子输入
                //m_playerRootViewPresenter.Disable( );               
            };
        }
        
        void InitView ( string windowName ) {
            // ui game obj
            m_windowViewPresenter = Util.CreateView(windowName).GetComponent<UguiWindowViewPresenter>( );
            //=========== ui event===========
            // New Game event
            m_windowViewPresenter.Button_NewGame.Clicked += ( ) => {
                m_gameModel.NewGame( );
            };
            //
            m_windowViewPresenter.Label_Timer.Text = m_timer.GameClockTimeText;

            #region Start Pause game switch event

            m_windowViewPresenter.Button_StartPauseGame.StartOrPauseGameEvent += (isStartGame) => {
                UnityAction startAction = ( ) => {
                    m_gameModel.StartGame( );
                };
                UnityAction pauseAction = ( ) => {
                    m_gameModel.PauseGame( );
                };

                if (isStartGame) {
                    Util.CallUnityAction(startAction);
                } else {
                    Util.CallUnityAction(pauseAction);
                }
            };

            #endregion Start Pause game switch event

            CreateView( );
        }

        void CreateView ( ) {
            // player drag chess piece input view
            //m_playerRootViewPresenter = Util.CreateView("Red").GetComponent<PlayerRootViewPresenter>( );
            // ai move chess piece game obj view
            m_aiViewPresenter = Util.CreateView(MonoConst.SCRIPT_GAMEOBJ_NAME).GetComponent<AiViewPresenter>( );
            // chess game obj hide/show view
            m_chessViewPresenter = Util.CreateView(MonoConst.SCRIPT_GAMEOBJ_NAME).GetComponent<ChessViewPresenter>( );
        }
        
        // 注册玩家下完一步棋事件的回调处理
        void PlayerChessOnceMove ( ) {
            Action<int> playerOnceMoveFinish = (move) => {
                // 数据计算，玩家走了一步棋，判断走法是否合理
                m_playerModel.ChessMove(move);                
                // 重置计时器
                ResetTimer( );
            };
            Action<int> playerOnceMoveCaptured = (sqDst) => {
                // 隐藏被吃掉的棋子
                //m_chessViewPresenter.HideChess(sqDst);
            };

            //foreach (BoxCollider chessBox in m_playerRootViewPresenter.HashPlayerQiZis.Values) {
            //    GameObject chess = chessBox.gameObject;
            //    //PlayerDragViewPresenter playerDragViewPresenter = chess.GetComponent<PlayerDragViewPresenter>( );

            //    // 玩家下完一步棋事件的回调
            //    playerDragViewPresenter.PlayerChessOnceMoveFinishEvent += playerOnceMoveFinish;
            //    // 玩家下完一步棋，吃子事件的回调
            //    playerDragViewPresenter.PlayerHasCapturedEvent += playerOnceMoveCaptured;
            //}
        }

        // 电脑下完一步棋
        void AiChessOnceMove ( ) {
            m_aiViewPresenter.AiOnceMoveFinishEvent += ( ) => {
                m_playerModel.SwitchPlayChess( );
                // 启用玩家走棋drag棋子输入
                //m_playerRootViewPresenter.Enable( );
                // 重置计时器
                ResetTimer( );
            };
        }

        // 重置计时器
        void ResetTimer ( ) {
            m_timer.ResetTimer( );
        }

    }
}